#ifndef VIDEOFORM_H
#define VIDEOFORM_H

#include <QPointer>
#include <QWidget>
#include <QSystemTrayIcon>
#include <QMenu>
#include <QGSettings>
#include "loadanimation.h"

namespace Ui
{
class videoForm;
}

enum WindowEdge { TOP = 12, LEFT = 21, CENTER = 22, RIGHT = 23, BUTTOMLEFT = 31, BUTTOM = 32, BUTTOMRIGHT = 33 };

struct AVFrame;
class ToolForm;
class BaseDevice;
// class FileHandler;
class QYUVOpenGLWidget;
class QLabel;
class VideoForm : public QWidget
{
    Q_OBJECT
public:
    explicit VideoForm(QWidget *parent = nullptr, int screenIndex = 0);

    ~VideoForm();

    void updateShowSize(const QSize &newSize);

    void updateRender(const AVFrame *frame);

    void setDevice(BaseDevice *device);

    QRect getGrabCursorRect();

    const QSize &frameSize();

    // 工具栏
    void showToolForm(bool show);

    void deleteUI();

public Q_SLOTS:
    void onToolFormStatusChange();

    void onMaxButtonClick(bool max);

    void onFullScreen();
    void onScrollTrigger();

    void onGetInputState();
    void onGetInputPos(int x, int y);
    void onLostInputState();

private:
    void initUI();

    // 注册信号
    void initConnect();

    // 移动窗口位置
    void moveCenter();

    // 注册快捷键
    void installShortcut();

    void slotActivated(QSystemTrayIcon::ActivationReason reason);

    // 设置鼠标形状
    void setCursorShape(int mPos);

    // 计算鼠标光标位置
    int calCursorPos(QPoint pt, int colPos);

    int calCursorX(QPoint pt);

    // 窗口缩放
    void windowZoom();

    // 设置窗口大小
    void setWindowSize();

    void changeInputBoxPos();

    QPoint cursorPoint();
    QRect getScreenRect();
    void setScreenIndex();
    void saveWindowConfig();
    void initPanelSetting();

protected:
    bool eventFilter(QObject *watch, QEvent *event) override;

private:
    // ui
    Ui::videoForm *ui;
    QPointer<ToolForm> m_toolForm;
    QPointer<LoadAnimation> m_loadingWidget;
    QPointer<QYUVOpenGLWidget> m_videoWidget;

    // inside member
    QSize m_frameSize;
    QPoint m_dragPosition;
    float m_widthHeightRatio = 0.5f;
    bool resizeFlag = false;  //窗口大小重置标志
    const int frameShape = 6; //用于鼠标区域判断
    int m_cursorPos;          // 鼠标在窗口的位置
    QPoint m_viewMousePos;    // 鼠标在全局的位置
    QRect m_preGeometry;      // 每次窗口的坐标和宽高
    int m_titleHeight;        // 标题栏与导航栏高度

    bool m_isVertical = false;    // 手机横竖屏
    bool m_isToolFormShow = true; // 是否展示导航栏
    bool m_isMax = false;         // 窗口最大化
    bool m_isFull = false;        // 窗口全屏
    bool m_isMousePress = false;  // 鼠标按下
    QPoint m_startWindowPos;
    QPoint m_beforePoint;
    QSize m_beforeSize;
    bool m_beforeIsVertical;
    int m_inputX = 0;
    int m_inputY = 0;
    int m_screenIndex = 0;
    QGSettings *m_panelSetting = nullptr;
    // outside member
    QPointer<BaseDevice> m_device;
};

#endif // VIDEOFORM_H
